home *** CD-ROM | disk | FTP | other *** search
-
- /* RETIRE Remove entries from User-Startup ***RGR***
- 01-may-95
- Author: Ralf Gruner, An der Sense 5a, D-02779 Großschönau, Germany.
-
- MANX Aztec C 5.2:
- cc retire
- ln retire -lc
- */
-
- //#define DEBUG
-
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- #define EOL 10
-
- char *filename1="S:User-Startup", *filename2="RAM:User-Startup";
- char *SalvageMessage="\nYour file \"S:User-Startup\" may be damaged.\nYou can found a copy of it in your Ram Disk.\nPlease save it NOW to a disk!\n";
- char *RAMerrorMsg="Error in RAM-Disk.";
- FILE *infile=0, *outfile=0;
- char *help="\nRETIRE removes entries from User-Startup.\nUsage: retire <Application 1> [<Application 2> ... [<Application n>]...]\n\n";
- char *version="$VER: RETIRE 1.2 (01.05.95)";
- char *application;
- int i,j,k,l;
- char line[256], *lineptr;
- int c=0;
-
- long ftell();
-
- /*************/
- void CloseAndExit(char *Msg)
- {
- if (infile) fclose(infile);
- if (outfile) fclose(outfile);
- if (Msg!=0L)
- {
- puts(Msg);
- exit(10);
- }
- remove(filename2);
- exit(0);
- }
-
- /*************/
- void ReadTextLine(void)
- {
- for(k=0; k<256; k++)
- line[k]='\0'; // clear line buffer
-
- k=0;
- while((c=fgetc(infile))!=EOF) // read line
- {
- line[k++]=c;
-
- #ifdef DEBUG
- if(c==EOL) printf("¶");
- printf("%c",c);
- #endif
-
- if(c==EOL || c==EOF || k>=256) break;
- }
- }
-
- /*************/
- void SkipSpaces(void)
- {
- while((*lineptr==' ' || *lineptr=='\t') && lineptr<&line[255])
- lineptr++;
- }
-
- /*************/
- main(int argc, char *argv[])
- {
- long beginpos;
- int ok=0, EmptyLine=0, AlreadyEmptyLine=0;
-
- if (*argv[1] == '?')
- {
- printf("\nRETIRE v%s Author: Ralf Gruner, Großschönau, Germany.\n",version+13);
- printf(help);
- exit(0);
- }
-
- if (argc < 2)
- {
- printf("\nMissing application name.\n");
- CloseAndExit(help);
- }
- else
- {
- for(i=1; i<=argc-1; i++)
- {
- #ifdef DEBUG
- printf("\nPass %d: %s\n",i,argv[i]);
- #endif
-
- if(!(infile=fopen(filename1,"r")))
- {
- printf("%s does not exist.\n",filename1);
- CloseAndExit(0L);
- }
-
- if(!(outfile=fopen(filename2,"w")))
- {
- printf("\nCould not open %s.\n",filename2);
- CloseAndExit("");
- }
-
- while((c=fgetc(infile))!=EOF) // copy s:user-startup into ram-disk
- {
- if(fputc(c, outfile)==EOF)
- CloseAndExit("\nError while writing.\n");
- }
-
- if(fclose(infile)==0)
- infile=0;
- else
- {
- printf("\nCould not close %s.\n",filename1);
- CloseAndExit("");
- }
- if(fclose(outfile)==0)
- outfile=0;
- else
- {
- printf("\nCould not close %s.\n",filename2);
- CloseAndExit("");
- }
-
-
- if(!(infile=fopen(filename2,"r")))
- {
- printf("\nCould not open %s.\n",filename2);
- CloseAndExit("");
- }
-
- application=argv[i]; // application name
- c=0;
- ok=0;
-
- while(c!=EOF && ok==0) // search current application name
- {
- ReadTextLine();
- ok=0;
- if(strnicmp(&line[0],";BEGIN ",7)==0)
- {
- lineptr=&line[7];
- SkipSpaces();
- if(strnicmp(lineptr,application,strlen(application))==0)
- {
- lineptr+=strlen(application);
- SkipSpaces();
- if(*lineptr==EOL)
- {
- ok=1; // found application name
- }
- }
- }
- }
-
- if(0!=fseek(infile,0,0)) CloseAndExit(RAMerrorMsg);
-
- #ifdef DEBUG
- if(ok==1) printf("Found application \"%s\".\n",application);
- #endif
-
- if(ok==1)
- {
- // if application name exists, then write all lines without application entry
-
- if(!(outfile=fopen(filename1,"w")))
- {
- printf("\nCould not open %s for writing.\n",filename1);
- CloseAndExit("");
- }
-
- c=0;
- ok=0;
-
- while(c!=EOF)
- {
- beginpos=ftell(infile);
- ReadTextLine();
- ok=0;
- if(strnicmp(&line[0],";BEGIN ",7)==0)
- {
- lineptr=&line[7];
- SkipSpaces();
- if(strnicmp(lineptr,application,strlen(application))==0)
- {
- lineptr+=strlen(application);
- SkipSpaces();
- if(*lineptr==EOL)
- {
- ok=1;
- while(strnicmp(&line[0],";END ",5)!=0 && c!=EOF)
- {
- ReadTextLine();
- if(c==EOF)
- {
- ok=0;
- break;
- }
- }
- if(ok==1)
- {
- // found ;END
- lineptr=&line[5];
- SkipSpaces();
- if(strnicmp(lineptr,application,strlen(application))==0)
- {
- // found name
- lineptr+=strlen(application);
- SkipSpaces();
- if(*lineptr==EOL)
- {
- ok=1;
- }
- else
- {
- ok=0;
- }
- }
- else
- ok=0;
- }
- if(ok==0)
- {
- printf("\nError in structure of S:User-Startup:\n;BEGIN %s does not match %s\n",application,&line[0]);
- if(0!=fseek(infile,beginpos,0))
- puts(RAMerrorMsg);
- ReadTextLine(); // if END not valid, read BEGIN again
- }
- if(ok==1)
- {
- #ifdef DEBUG
- printf("(*** removed.***)\n");
- #endif
- printf("%s retired.\n",application);
- continue;
- }
- }
- }
- }
-
- lineptr=&line[0];
- SkipSpaces();
- if(*lineptr==EOL)
- EmptyLine=1;
- else
- EmptyLine=0;
- if(AlreadyEmptyLine==0 || EmptyLine==0)
- {
- l=0;
- while(l<k) // write line
- {
- c=line[l++];
- if(fputc(c, outfile)==EOF)
- {
- printf("\nError while writing %s.%s",filename1,SalvageMessage);
- CloseAndExit("");
- }
- }
- }
- AlreadyEmptyLine=EmptyLine;
- }
- if(fclose(outfile)==0)
- outfile=0;
- else
- {
- printf("\nCould not close %s.%s",filename1,SalvageMessage);
- CloseAndExit("");
- }
- }
- if(fclose(infile)==0)
- infile=0;
- else
- CloseAndExit(RAMerrorMsg);
- }
- }
- CloseAndExit(0L);
- }
-
-